<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="成果对应id" prop="achievementId">-->
<!--        <el-input-->
<!--            v-model="queryParams.achievementId"-->
<!--            placeholder="请输入成果对应id"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="标题" prop="title">-->
<!--        <el-input-->
<!--            v-model="queryParams.title"-->
<!--            placeholder="请输入标题"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="移除时间" prop="deletedAt">-->
<!--        <el-date-picker-->
<!--            v-model="queryParams.deletedAt"-->
<!--            type="date"-->
<!--            placeholder="选择移除时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['team:trash:remove']"
        >取消移除</el-button>
      </el-col>
<!--      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="trashList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="上传者" align="center" prop="nickName" />
      <el-table-column label="成果类型" align="center" prop="achievementType" />
      <el-table-column label="标题" align="center" prop="title" />
<!--      <el-table-column label="移除时间" align="center" prop="deletedAt" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.deletedAt, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['team:trash:remove']"
          >取消移除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改成果移除箱对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上传者id" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入上传者id" />
        </el-form-item>
        <el-form-item label="成果对应id" prop="achievementId">
          <el-input v-model="form.achievementId" placeholder="请输入成果对应id" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="移除时间" prop="deletedAt">
          <el-date-picker
              v-model="form.deletedAt"
              type="date"
              placeholder="选择移除时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listTrash, getTrash, delTrash, addTrash, updateTrash } from "@/api/team/trash"

const queryFormRef = ref(null)
const formRef = ref(null)
const loading = ref(false)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const open = ref(false)
const trashList = ref([])

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  orderByColumn: "deletedAt",
  isAsc: "desc",
  userid: null,
  achievementType: null,
  achievementId: null,
  title: null,
  deletedAt: null
})

const form = reactive({
  id: null,
  userid: null,
  achievementType: null,
  achievementId: null,
  title: null,
  deletedAt: null
})

const rules = {
  userid: [
    { required: true, message: "上传者id不能为空", trigger: "blur" }
  ],
  achievementType: [
    { required: true, message: "成果类型不能为空", trigger: "change" }
  ],
  achievementId: [
    { required: true, message: "成果对应id不能为空", trigger: "blur" }
  ],
  title: [
    { required: true, message: "标题不能为空", trigger: "blur" }
  ],
  deletedAt: [
    { required: true, message: "移除时间不能为空", trigger: "blur" }
  ]
}

onMounted(() => {
  getList()
})

/** 查询成果移除箱列表 */
function getList() {
  loading.value = true
  listTrash(queryParams).then(response => {
    trashList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.id = null
  form.userid = null
  form.achievementType = null
  form.achievementId = null
  form.title = null
  form.deletedAt = null
  formRef.value?.resetFields()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加成果移除箱"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getTrash(id).then(response => {
    Object.assign(form, response.data)
    open.value = true
    title.value = "修改成果移除箱"
  })
}

/** 提交按钮 */
function submitForm() {
  formRef.value?.validate(valid => {
    if (valid) {
      if (form.id != null) {
        updateTrash(form).then(response => {
          ElMessage.success("修改成功")
          open.value = false
          getList()
        })
      } else {
        addTrash(form).then(response => {
          ElMessage.success("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const trashIds = row.id || ids.value
  ElMessageBox.confirm('是否确认删除成果移除箱编号为"' + trashIds + '"的数据项？').then(() => {
    return delTrash(trashIds)
  }).then(() => {
    getList()
    ElMessage.success("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  // Implement your export functionality here
  console.log("Export functionality to be implemented")
}

</script>

<style scoped>
.app-container {
  padding: 20px;
}

.mb8 {
  margin-bottom: 8px;
}
</style>
